import React from 'react';
import ReactDOM from 'react-dom';
//import './index.css';
import App from './App';
import {createStore} from 'redux'
import {Provider} from 'react-redux';
import reducer from './reducers/counter'
import { HashRouter as Router,Route,Switch} from 'react-router-dom'
import {mainRoutes}from './router/routers'
import * as serviceWorker from './serviceWorker';
//  import Login from './pages/login/index'
//import NotFound from './pages/notfound/index'
const store=createStore(reducer);
var state=store.getState()
//console.log(state.isLogin,'我变化了',)

//var isLogin=false
// const c=function(){
//   let token=sessionStorage.getItem('token')||''
//   if(token){
//     isLogin=true
//   }else{
//     isLogin=false
//   }
//   console.log('我没有调用')
// }
// c()

const rou=function(){
  var flag=false
  let token=sessionStorage.getItem('token')||''
  if(token){
    flag=true
  }else{
    flag=false
  }
  //console.log(state.isLogin,'是true')
 if(flag){
  return(
    // <React.StrictMode>
 <Provider store={store}>
   {/* <BrowserRouter> */}
<Router>
<Switch>
  {/* <Route  component={Login}/> */}
 < Route path='/' render={routeProps=><App {...routeProps}/>}/>
  {/* {mainRoutes.map(route=>{
    return <Route key={route.path} {...route}/>
  })} */}
</Switch>
</Router>
{/* </BrowserRouter> */}
 </Provider>
)
 }else{
  return(
    // <React.StrictMode>
 <Provider store={store}>
   {/* <BrowserRouter> */}
<Router>
<Switch>
  {/* <Route  component={Login}/> */}
 {/* {state.isLogin? < Route path='/home' render={routeProps=><App {...routeProps}/>}/>:''} */}
  {mainRoutes.map(route=>{
    return <Route key={route.path} {...route}/>
  })}
</Switch>
</Router>
{/* </BrowserRouter> */}
 </Provider>
)
 }
}

// ReactDOM.render(
//   // <React.StrictMode>
//    <Provider store={store}>
// <Router>
// <Switch>
//     {/* <Route  component={Login}/> */}
//    {state.isLogin? < Route path='/home' render={routeProps=><App {...routeProps}/>}/>:mainRoutes.map(route=>{
//       return <Route key={route.path} {...route}/>
//     })}
//     {/* {mainRoutes.map(route=>{
//       return <Route key={route.path} {...route}/>
//     })} */}
//   </Switch>
// </Router>
//    </Provider>,
//   // </React.StrictMode>,
//   document.getElementById('root')
// );
store.subscribe(()=>{
  console.log(state.isLogin,'怎么了')
  rou()
  ReactDOM.render(
    rou(),
    document.getElementById('root')
  )
})
ReactDOM.render(
  rou(),
  document.getElementById('root')
)
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
